package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.FileTag;
import com.micro.ai.files.service.FileTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件标签控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/file-tags")
@Tag(name = "文件标签管理", description = "文件标签的增删改查等功能")
public class FileTagController {

    @Autowired
    private FileTagService fileTagService;

    @PostMapping
    @PreAuthorize("hasAuthority('file_tag:create')")
    @Operation(summary = "创建标签", description = "创建新的文件标签")
    public ApiResponse<FileTag> create(@Valid @RequestBody FileTag tag) {
        FileTag result = fileTagService.create(tag);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('file_tag:update')")
    @Operation(summary = "更新标签", description = "更新标签信息")
    public ApiResponse<FileTag> update(
            @Parameter(description = "标签ID", required = true) @PathVariable String id,
            @Valid @RequestBody FileTag tag) {
        FileTag result = fileTagService.update(id, tag);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('file_tag:delete')")
    @Operation(summary = "删除标签", description = "删除标签")
    public ApiResponse<Void> delete(
            @Parameter(description = "标签ID", required = true) @PathVariable String id) {
        fileTagService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('file_tag:view')")
    @Operation(summary = "获取标签详情", description = "根据ID获取标签详细信息")
    public ApiResponse<FileTag> getById(
            @Parameter(description = "标签ID", required = true) @PathVariable String id) {
        FileTag tag = fileTagService.getById(id);
        if (tag == null) {
            return ApiResponse.error("F0002", "标签不存在");
        }
        return ApiResponse.success(tag);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('file_tag:view')")
    @Operation(summary = "分页查询标签", description = "支持多条件查询标签列表")
    public ApiResponse<Page<FileTag>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "关键词") @RequestParam(value = "keyword", required = false) String keyword) {
        Page<FileTag> page = fileTagService.pageQuery(pageNum, pageSize, tenantId, keyword);
        return ApiResponse.success(page);
    }

    @GetMapping("/all")
    @PreAuthorize("hasAuthority('file_tag:view')")
    @Operation(summary = "查询所有标签", description = "查询租户下的所有标签")
    public ApiResponse<List<FileTag>> listAll(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId) {
        List<FileTag> list = fileTagService.listAll(tenantId);
        return ApiResponse.success(list);
    }

    @GetMapping("/by-name")
    @PreAuthorize("hasAuthority('file_tag:view')")
    @Operation(summary = "根据标签名称查询", description = "根据标签名称查询标签")
    public ApiResponse<FileTag> getByTagName(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "标签名称", required = true) @RequestParam("tagName") String tagName) {
        FileTag tag = fileTagService.getByTagName(tenantId, tagName);
        if (tag == null) {
            return ApiResponse.error("F0002", "标签不存在");
        }
        return ApiResponse.success(tag);
    }
}

